//index.js
//获取应用实例
const app = getApp()
let echarts = app.echarts;
function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  console.log(width,height,'-----')
  canvas.setChart(chart);
  var rate = 30;
  var data = {
    max: 300,
    rate: 20
  }
  var labelArr = [0, 10, 100, 240, 300];
  var endFix = '';
  var res = wx.getSystemInfoSync();
  var windowWidth = res.windowWidth;
  var option = {
    tooltip: {
      formatter: parseFloat(rate)
    },
    series: [{
      name: '业务指标',
      type: 'gauge',
      startAngle: 180,
      endAngle: 0,
      
      radius: "182%",
      center: ['50%', '92%'],    // 默认全局居中
      axisLine: {
        show: true,
        // 属性lineStyle控制线条样式
        // 按比例填充 总刻度 100
        lineStyle: {
          width: 25,
          color: [[0.18, '#50b025'], [0.34, '#85e861'], [0.50, '#f7f600'], [0.66, '#f5d500'], [0.82, '#f07800'], [1, '#e92200']]
        }
      },
      splitLine: {
        show:false,
      },
      axisTick: {
        show: false,
      },
      axisLabel: {
        show: true,
      },
      pointer: {
        // length: 56 * windowWidth / 375,
        // width: 6 * windowWidth / 375,
        // color:"#4a90e2"
        "width": 6, //指针的宽度
        "length": "60%", //指针长度，按照半圆半径的百分比
        "color": "auto"
      },
      detail: {
        offsetCenter: [0, '-120%'],
        fontSize: 1,
        formatter: '{value}'
      },
      data: [
        { value: 60 }
      ]
    }]
  };

  chart.setOption(option, true);

  return chart;
}
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    tabIndex:0,
    xData:[
      { color:'#50b025',text:'理想血压'},
      { color: '#85e861', text: '正常血压' },
      { color: '#f7f600', text: '正常偏高' },
      { color: '#f5d500', text: '轻度高压' },
      { color: '#f07800', text: '中度血压' },
      { color: '#e92200', text: '严重血压' },
    ],
    ec: {
      onInit: initChart
    }
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
     
      // wx.navigateTo({
      //   url: '../login/index',
      // })
    if (app.globalData.userInfo) {
      console.log('index---1')
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      console.log('index---2')
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        console.log('2222',res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      console.log('index---3')
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          console.log('index---33')
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  // 用户授权
  bindGetUserInfo(e){
    var that = this;
    if (e.detail.userInfo) {
      getApp().getUser(function () {
        that.setData({
          hasUserInfo: true,
        })
      });
    }else{
      wx.showToast({
        title: '授权失败',
        icon:'none',
      })
      setTimeout(function(){
        wx.navigateTo({
          url: '/pages/login/index',
        })
      },200)
    }
  },
  // 开始、结束 按钮
  startBtn(){
    console.log('开始按钮')
  },
  endBtn(){
    console.log('结束按钮')
  }
})
